package com.jx.jingxuan.controller;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.jx.jingxuan.R;
import com.jx.jingxuan.base.BaseActivity;
import com.jx.jingxuan.controller.home.HomeFragment;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * <li>Package:com.jx.jingxuan.controller.home</li>
 * <li>Author: Administrator  </li>
 * <li>Date: 2019/3/18</li>
 * <li>Description:   </li>
 */
public class TabHomeActivity extends BaseActivity implements RadioGroup.OnCheckedChangeListener {
    @BindView(R.id.tabBar)
    RadioGroup tabBar;
    @BindView(R.id.layout_container)
    FrameLayout layoutContainer;
    private Fragment currentFragment;

    private HomeFragment homeFragment;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        ButterKnife.bind(this);

        tabBar.setOnCheckedChangeListener(this);

        homeFragment = new HomeFragment();
        showFragment(homeFragment);
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.tab_home:
                showFragment(homeFragment);
                break;
            case R.id.tab_jingxuan:

                break;
            case R.id.tab_member:

                break;
            case R.id.tab_news:

                break;
            case R.id.tab_my:

                break;
        }
    }

    // 显示指定的fragment
    public void showFragment(Fragment fragment) {

        if (fragment == null) {
            return;
        }

        if (currentFragment != null) {
            if (currentFragment == fragment) {
                return;
            }
        }

        // 获取选择  当前选种的Fragment
        FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();

        // 没有增加的都增加进来
        if (!fragment.isAdded()) {
            transaction.add(R.id.layout_container, fragment, fragment.getClass().getSimpleName());
        }

        // 判断当前是否已经有显示的fragment
        if (currentFragment != null) {
            transaction.hide(currentFragment);
        }

        transaction.show(fragment);
        transaction.commitAllowingStateLoss();

        currentFragment = fragment;
    }
}
